package com.lyc.product.daoIpml;

import com.lyc.user.util.jdbcUtil;
import com.lyc.product.bean.Product;
import com.lyc.product.dao.ProductDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    /**
     * 查询所有的书籍信息
     *
     * @return
     */
    @Override
    public List<Product> getBookLisst() {
        Connection connection = null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        List<Product> products=new ArrayList<Product>();
        try {
            connection = jdbcUtil.getConnection();
            String sql="select productId,productImgurl,price,author from product where state='2' ";
            ps = connection.prepareStatement(sql);
            resultSet = ps.executeQuery();

            while (resultSet.next()){
                Product product=new Product();
                product.setProductId(resultSet.getInt("productId"));
                product.setProductImgurl(resultSet.getString("productImgurl"));
                product.setPrice(resultSet.getDouble("price"));
                product.setAuthor(resultSet.getString("author"));
                products.add(product);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return products;

    }

    /**
     * 根据指定页数查找符合条件的书籍信息
     * @param startIndex 查找开始的下标位置
     * @param PageRecordCount 每页显示的记录数
     * @return
     */
    @Override
    public List<Product> getBookListByPage(int startIndex,int PageRecordCount) {
        Connection connection = null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        List<Product> products=new ArrayList<Product>();
        try {
            connection = jdbcUtil.getConnection();
            String sql="select id,productId,productImgurl,price,author from product where state='2' order by productId limit ?,? ";
            ps = connection.prepareStatement(sql);
            ps.setInt(1,startIndex);
            ps.setInt(2,PageRecordCount);
            resultSet = ps.executeQuery();

           while (resultSet.next()){
               Product product = new Product();
               product.setId(resultSet.getInt("id"));
               product.setProductId(resultSet.getInt("productId"));
               product.setProductImgurl(resultSet.getString("productImgurl"));
               product.setPrice(resultSet.getDouble("price"));
               product.setAuthor(resultSet.getString("author"));
               products.add(product);
           }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return products;
    }

    /**
     * 通过商品编号查询书籍
     * @param productId
     * @return
     */
    @Override
    public Product getBookByProductId(int productId) {
        Connection connection = null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        Product product=null;
        try {
            connection = jdbcUtil.getConnection();
            String sql="select id,productId,productName,numbers,discount,press,productImgurl,price,author ,publicationTime from product where state=2 and productId = ? ";
            ps = connection.prepareStatement(sql);
            ps.setInt(1,productId);
            resultSet = ps.executeQuery();

          if (resultSet.next()){
                product=new Product();
                product.setId(resultSet.getInt("id"));
                product.setProductId(resultSet.getInt("productId"));
                product.setProductName(resultSet.getString("productName"));
                product.setNumbers(resultSet.getInt("numbers"));
                product.setDiscount(resultSet.getDouble("discount"));
                product.setPress(resultSet.getString("press"));
                product.setProductImgurl(resultSet.getString("productImgurl"));
                product.setPrice(resultSet.getDouble("price"));
                product.setAuthor(resultSet.getString("author"));
                product.setPublicationTime(resultSet.getDate("publicationTime"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return product;
}
}
